package com.drinks.yiniang.ui.widget.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.hansen.library.utils.ScreenSizeUtils;

/**
 * 添加标签里需要的呼吸灯
 * @author han
 * @date 2019/12/23
 */
public class BreathingTagView extends View {

    private final int DEFAULT_SIZE;
    private final int DEFAULT_INNER_SIZE;

    private Paint mPaint;

    private int mOuterColor;
    private int mInnerColor;

    private float mScaleRatio = 1f;

    // 呼吸灯动画
    private ValueAnimator mBreathingAnimator;

    public BreathingTagView(Context context) {
        this(context, null);
    }

    public BreathingTagView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BreathingTagView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        DEFAULT_SIZE = ScreenSizeUtils.dp2px(context, 12);
        DEFAULT_INNER_SIZE = ScreenSizeUtils.dp2px(context, 5);

        mOuterColor = Color.parseColor("#99646060");
        mInnerColor = Color.WHITE;

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centX = getWidth() / 2;
        int centY = getHeight() / 2;

        //画外圆
        mPaint.setColor(mOuterColor);
        canvas.drawCircle(centX, centY, DEFAULT_SIZE / 2 * mScaleRatio, mPaint);
        //画内圆
        mPaint.setColor(mInnerColor);
        canvas.drawCircle(centX, centY, DEFAULT_INNER_SIZE / 2, mPaint);
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //设置一个默认值，就是这个View的默认宽度为1，这个看我们自定义View的要求
        int result = DEFAULT_SIZE;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }

        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = DEFAULT_SIZE;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }

        return result;
    }

    /**
     * 开启呼吸灯动画 注动画无线循环注意回收防止内存泄露
     */
    public void startBreathingAnimator() {
        clearBreathingAnimator();

        mBreathingAnimator = ValueAnimator.ofFloat(0.8F, 1.0F);
        mBreathingAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mBreathingAnimator.setDuration(800);
        mBreathingAnimator.setStartDelay(200);
        mBreathingAnimator.setRepeatCount(-1);
        mBreathingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mScaleRatio = value;
                postInvalidate();
            }
        });
        mBreathingAnimator.start();
    }

    // 注意清理 防止内存泄露
    private void clearBreathingAnimator() {
        if (mBreathingAnimator != null && mBreathingAnimator.isRunning()) {
            mBreathingAnimator.cancel();
            mBreathingAnimator = null;
        }
    }

    public int getInnerSize() {
        return DEFAULT_INNER_SIZE;
    }

    @Override
    protected void onDetachedFromWindow() {
        clearBreathingAnimator();
        super.onDetachedFromWindow();
    }
}
